const util = {
	loading(title = '加载中') {
		uni.showLoading({
			title
		})
	},
	toast(title = '', icon = 'none') {
		return uni.showToast({
			title,
			icon
		})
	},
	getHeader() {
		return {
			'Authorization': uni.getStorageSync('userToken')
		}
	},
	turnTime_Java(time_str, type) {
		const date = new Date(time_str);
		const year = date.getFullYear();
		const month = (date.getMonth() + 1).toString().padStart(2, '0');
		const day = date.getDate().toString().padStart(2, '0');
		const hours = date.getHours().toString().padStart(2, '0');
		const minutes = date.getMinutes().toString().padStart(2, '0');
		const seconds = date.getSeconds().toString().padStart(2, '0');

		switch (type) {
			case "seconds":
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			case "minutes":
				return `${year}-${month}-${day} ${hours}:${minutes}`;
			case "hours":
				return `${year}-${month}-${day} ${hours}`;
			case "day":
				return `${year}-${month}-${day}`;
			case "month":
				return `${year}-${month}`;
			case "year-alone":
				return `${year}`;
			case "month-alone":
				return `${month}`;
			case "day-alone":
				return `${day}`;
			case "date-md":
				return `${month}-${day}`;
			case "time-hm":
				return `${hours}:${minutes}`;
			case "date-mdhm":
				return `${month}-${day} ${hours}:${minutes}`;
			case "time-hms":
				return `${hours}:${minutes}:${seconds}`;
			default:
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
		}

		return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

	},
	getTimediffer(timeStr) {
		const currentDate = new Date(); // 当前时间
		const targetDate = new Date(timeStr);
		const oneDayMs = 24 * 60 * 60 * 1000; // 一天的毫秒数
		const oneHoursMs = 60 * 60 * 1000; // 一时的毫秒数
		const oneMinutesMs = 60 * 1000; // 一分的毫秒数
		const oneSecondsMs = 1000; // 一秒的毫秒数
		const diffDays = Math.floor((currentDate.getTime() - Number(timeStr)) / oneDayMs);
		// console.log(currentDate.getTime())
		// console.log(targetDate.getTime())
		// console.log(currentDate)
		// console.log(currentDate.getTime() - Number(timeStr))
		// console.log(diffDays)
		if (diffDays === 0) {
			const diffHours = Math.floor((currentDate.getTime() - Number(timeStr)) / oneHoursMs);
			if (diffHours === 0) {
				const diffMinutes = Math.floor((currentDate.getTime() - Number(timeStr)) / oneMinutesMs);
				// console.log(diffMinutes)
				if (diffMinutes === 0) {
					const diffSeconds = Math.floor((currentDate.getTime() - Number(timeStr)) / oneSecondsMs);
					return diffSeconds + '秒';
				} else {

					return diffMinutes + '分钟';
				}
			} else {
				return diffHours + '小时';

			}

		} else {
			return diffDays + '天';
		}

	},
	// //获取登录用户的昵称
	getUsername() {

		return 'hhh';
	},

	preloadImage(url) {
		return new Promise((resolve, reject) => {
			const img = new Image();
			img.onload = () => {
				resolve(url);
			};
			img.onerror = () => {
				reject(new Error(`Failed to load image: ${url}`));
			};
			img.src = url;
		});
	}


}
export default util